package com.fzu.normalservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzu.commonutil.entity.ServiceType;
import com.fzu.commonutil.util.CodeEnum;
import com.fzu.commonutil.entity.Company;
import com.fzu.commonutil.mapper.CompanyMapper;
import com.fzu.commonutil.util.PageVO;
import com.fzu.normalservice.param.RegisterParam;
import com.fzu.normalservice.service.CompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzu.normalservice.service.UserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyq
 * @since 2023-03-26
 */
@Service
@RequiredArgsConstructor
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {
    private final UserService userService;
    @Override
    public List<Company> options(Company company) {
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
        Integer state = company.getState();
        if (state != null) {
            wrapper.eq(Company::getState, CodeEnum.NORMAL.getCode());
        }
        return super.list(wrapper);
    }

    @Override
    public Map<Integer, String> map(Collection<Integer> idList) {
        Map<Integer, String> map = new HashMap<>();
        if (!CollectionUtils.isEmpty(idList)) {
            Collection<Company> companies = super.listByIds(idList);
            map = companies.stream().collect(Collectors.toMap(Company::getId, Company::getName));
        }
        return map;
    }

    @Override
    public List<Company> list(Company company) {
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
        String name = company.getName();
        if (StringUtils.isNotBlank(name)) {
            wrapper.eq(Company::getName, name);
        }
        Integer state = company.getState();
        if (state != null) {
            wrapper.eq(Company::getState, state);
        }
        return super.list(wrapper);
    }

    @Override
    public PageVO page(Company company, Integer pageNum, Integer pageSize) {
        // 构造分页参数
        Page<Company> pageRequest = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();

        String name = company.getName();
        if(StringUtils.isNotBlank(name)){
            wrapper.eq(Company::getName, name);
        }

        IPage<Company> page = super.page(pageRequest, wrapper);
        return PageVO.convert(page);
    }

    @Override
    public Integer getAdminByCompanyName(String companyName) {
        LambdaQueryWrapper<Company> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Company::getName, companyName);
        Company company = getOne(lambdaQueryWrapper);
        return company.getUserId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Company company) {
        company.setState(CodeEnum.ENABLE.getCode());
        super.save(company);
        // 注册管理员账号
        RegisterParam registerParam = new RegisterParam();
        registerParam.setAccount(company.getAccount());
        registerParam.setPassword(company.getPassword());
        registerParam.setNickName(company.getPrincipal());
        registerParam.setRole(CodeEnum.COMPANY.getCode());
        registerParam.setCompanyId(company.getId());
        Integer userId = userService.register(registerParam);
        company.setUserId(userId);
        super.updateById(company);
    }

}
